define(['zepto', 'utils', 'JST', 'sui', 'submitForm'], function($, utils, JST){
  return {
    init: function() {
      this.initPicker();
      
      this.$editPosture = $('[href="#edit-posture"]')
      this.$editJoint = $('[href="#edit-joint"]')
      this.$editSummary = $('[href="#edit-summary"]')
      this.$medicalHistory = $('[href="#medical-history"]')
      this.$sportsHistory = $('[href="#sports-history"]')
      this.$completeBreakfast= $('[href="#breakfast"]')
      this.$completeLunch = $('[href="#lunch"]')
      this.$completeDinner = $('[href="#dinner"]')
      this.$otherFood = $('[href="#other-food"]')
      this.$otherItem = $('[href="#other"]')
      
      this.$editPosture.length ? this.bindEditPosture() : void 0
      this.$editJoint.length ? this.bindEditJoint() : void 0
      this.$editSummary.length ? this.bindEditSummary() : void 0
      this.$medicalHistory.length ? this.bindMedicalHistory() : void 0
      this.$sportsHistory.length ? this.bindSportsHistoryAction() : void 0
      this.$completeBreakfast.length ? this.bindCompleteFood(this.$completeBreakfast) : void 0
      this.$completeLunch.length ? this.bindCompleteFood(this.$completeLunch) : void 0
      this.$completeDinner.length ? this.bindCompleteFood(this.$completeDinner) : void 0
      
      this.$otherFood.length ? this.bindOtherFood(this.$otherFood) : void 0
      this.$otherItem.length ? this.bindOtherItem(this.$otherItem) : void 0
      
      this.initSubmit();
     
    },
    
    bindEditPosture: function() {
      var that = this;
      that.$editPosture.on("click", function(e) {
        e.preventDefault();
        
        var $page = $(JST.editSummaryTpl({
          'title': '体态总结',
          'placeholder': ''
        }))
        that.bindEditSummaryPageEvent($page, that.$editPosture)
        $.push.transitionIn($page)
        
      })
    },
    
    bindEditJoint: function() {
      var that = this;
      that.$editJoint.on("click", function(e) {
        e.preventDefault();
        var $page = $(JST.editSummaryTpl({
          'title': '关节总结',
        }))
        that.bindEditSummaryPageEvent($page, that.$editJoint)
        $.push.transitionIn($page)
      })
    },
    
    bindEditSummary: function() {
      var that = this;
      that.$editSummary.on("click", function(e) {
        e.preventDefault();
        var $page = $(JST.editSummaryTpl({
          'title': '总结',
        }))
        that.bindEditSummaryPageEvent($page, that.$editSummary)
        $.push.transitionIn($page)
      })
    },
    
    bindEditSummaryPageEvent: function($page, element) {
      var that = this;
      $page.on("click", "#submit", function(e) {
        e.preventDefault();
        var itemDomString = $page.find("textarea").val().trim();
          
        $.push.transitionOut($page, {
          beforeTransition: function() {
            $(element).parent().find(".description")
              .empty().html(itemDomString)
          }
        })
      })
      
      return $page
    },
    
    bindMedicalHistory: function() {
      var that = this
      
      that.$medicalHistory.on("click", function(e) {
        e.preventDefault();
        var defer = that.compileMedicalHistory();
        $.when(defer).done(function($page) {
          $.push.transitionIn($page)
        })
      })
    },
    
    bindCompleteFood: function(element) {
      var that = this
      $(element).on("click", function(e) {
        e.preventDefault();
        var defer = that.compileFood(element);
        $.when(defer).done(function($page) {
          $.push.transitionIn($page)
        })
      })
    },
    
    bindOtherFood: function(element) {
      var that = this
      $(element).on("click", function(e) {
        e.preventDefault();
        that.$otherFoodAction = $(e.currentTarget)
        var defer = that.compileOtherFood();
        $.when(defer).done(function($page) {
          $.push.transitionIn($page)
        })
      })
    },
    
    compileOtherFood: function(element) {
      var that = this;
      var $page = $(JST.otherFoodTpl())
      
      $page.on("click", "#submit", function(e) {
        e.preventDefault();
        var itemDomString = $page.find("textarea").val().trim();
          
        $.push.transitionOut($page, {
          beforeTransition: function() {
          that.$otherFoodAction.empty().html(itemDomString)
          }
        })
      })
      
      return $page
    },
    
    bindOtherItem: function(element) {
      var that = this
      $(element).on("click", function(e) {
        e.preventDefault();
        that.$otherItemAction = $(e.currentTarget)
        var defer = that.compileOtherItem();
        $.when(defer).done(function($page) {
          $.push.transitionIn($page)
        })
      })
    },
    
    compileOtherItem: function(element) {
      var that = this;
      var $page = $(JST.otherItemTpl())
      
      $page.on("click", "#submit", function(e) {
        e.preventDefault();
        var itemDomString = $page.find("textarea").val().trim();
          
        $.push.transitionOut($page, {
          beforeTransition: function() {
          that.$otherItemAction.empty().html(itemDomString)
          }
        })
      })
      
      return $page
    },
    
    
    compileMedicalHistory: function() {
      var that = this;
      
      var $page = $(JST.medicalHistoryTpl())
      that.bindMedicalHistoryPageEvent($page)
      return $page
    },
    
    compileFood: function(element) {
      var that = this;
      
      var $page = $(JST.completeFoodTpl())
      that.bindCompleteFoodPageEvent(element, $page)
      return $page
    },
    
    bindMedicalHistoryPageEvent: function($page) {
      var that = this;
      $page.on("click", "#submit", function(e) {
        e.preventDefault();
        var itemDomString = $page.find("textarea").val().trim();
          
        $.push.transitionOut($page, {
          beforeTransition: function() {
            that.$medicalHistory
              .empty().html(itemDomString)
          }
        })
      })
    },
    
    bindCompleteFoodPageEvent: function(element, $page) {
      var that = this;
      $page.on("click", "#submit", function(e) {
        e.preventDefault();
        var itemDomString = $page.find("textarea").val().trim();
          
        $.push.transitionOut($page, {
          beforeTransition: function() {
            element.find(".descript")
              .empty().html(itemDomString)
          }
        })
      })
    },
    
    bindSportsHistoryAction: function() {
      var that = this
      
      that.$sportsHistory.on("click", function(e) {
        e.preventDefault();
        var defer = that.compileSportsHistory();
        $.when(defer).done(function($page) {
          $.push.transitionIn($page)
        })
      })
    },
    
    compileSportsHistory: function() {
      var that = this;
      
      var $page = $(JST.sportsHistoryTpl())
      that.bindSportsHistoryPageEvent($page)
      return $page
    },
    
    bindSportsHistoryPageEvent: function($page) {
      var that = this;
      $page.on("click", "#submit", function(e) {
        e.preventDefault();
        var itemDomString = [];
        
        $page.find("[type='checkbox']:checked").each(function() {
          itemDomString.push($(this).val())
        });
        
        var itemString = itemDomString.join(",")
          
        $.push.transitionOut($page, {
          beforeTransition: function() {
            that.$sportsHistory.find("div").empty().html(itemString)
          }
        })
      })
    },
    
    initPicker: function() {
      var that = this;
      
      $('[data-name="burpee"]').picker({
        toolbarTemplate: '<header class="bar bar-nav">\
          <button class="button button-link pull-left">取消</button>\
          <button class="button button-link pull-right close-picker">确定</button>\
          </header>',
          cols: [
            {
              textAlign: 'center',
              values: ['标准', '简化'],
              displayValues: ['标准', '简化']
            },
            {
              textAlign: 'center',
              values: ['30', '31', '32','33','34','35','36','37','38','39','40'],
              displayValues: ['30个', '31个', '32个','33个','34个','35个','36个','37个','38个','39个','40个']
            }
          ],
          cssClass: "evaluation-bar",
          formatValue: function (picker, value, displayValue){
            $(this.input).parent().find('input[type="hidden"]').val(value)
            return displayValue
          },
        });
      
      $('[data-name="picker1"]').picker({
        toolbarTemplate: '<header class="bar bar-nav">\
          <button class="button button-link pull-left">取消</button>\
          <button class="button button-link pull-right close-picker">确定</button>\
          </header>',
          cols: [
            {
              textAlign: 'center',
              values: ['<30', '30-60', '61-90', '>90'],
              displayValues: ['<30s', '30-60s', '61-90s', '>90s']
            }
          ],
          formatValue: function (picker, value, displayValue){
            $(this.input).parent().find('input[type="hidden"]').val(value)
            return displayValue
          },
        });
      
      $('[data-name="picker2"]').picker({
        toolbarTemplate: '<header class="bar bar-nav">\
          <button class="button button-link pull-left">取消</button>\
          <button class="button button-link pull-right close-picker">确定</button>\
          </header>',
          cols: [
            {
              textAlign: 'center',
              values: ['<10', '10-20', '20-30', '>30'],
              displayValues: ['<10', '10-20', '20-30', '>30']
            }
          ],
          formatValue: function (picker, value, displayValue){
            $(this.input).parent().find('input[type="hidden"]').val(value)
            return displayValue
          },
        });
      
      $('[data-name="picker3"]').picker({
        toolbarTemplate: '<header class="bar bar-nav">\
          <button class="button button-link pull-left">取消</button>\
          <button class="button button-link pull-right close-picker">确定</button>\
          </header>',
        cols: [
          {
            textAlign: 'center',
            values: ['<10', '10-15', '16-20', '>20'],
            displayValues: ['<10', '10-15', '16-20', '>20']
          }
        ],
        formatValue: function (picker, value, displayValue){
          $(this.input).parent().find('input[type="hidden"]').val(value)
          return displayValue
        },
      });
      
      $('[data-name="mom-students"]').picker({
        toolbarTemplate: '<header class="bar bar-nav">\
          <button class="button button-link pull-left">取消</button>\
          <button class="button button-link pull-right close-picker">确定</button>\
          </header>',
        cols: [
          {
            textAlign: 'center',
            values: ['正常', '1指', '2指', '3指', '3指以上'],
            displayValues: ['正常', '1指', '2指', '3指', '3指以上']
          }
        ],
        formatValue: function (picker, value, displayValue){
          $(this.input).parent().find('input[type="hidden"]').val(value)
          return displayValue
        },
      });
      
      $('[data-name="direction"]').picker({
        toolbarTemplate: '<header class="bar bar-nav">\
          <button class="button button-link pull-left close-picker">取消</button>\
          <button class="button button-link pull-right close-picker">确定</button>\
          </header>',
        cols: [
          {
            textAlign: 'center',
            values: ['正常', '内扣', '外扣'],
            displayValues: ['正常', '内扣', '外扣']
          },
        ],
        formatValue: function (picker, value, displayValue){
          $(this.input).parent().find('input[type="hidden"]').val(value)
          return displayValue
        },
      });
      
      $('[data-name="parallel"]').picker({
        toolbarTemplate: '<header class="bar bar-nav">\
          <button class="button button-link pull-left">取消</button>\
          <button class="button button-link pull-right close-picker">确定</button>\
          </header>',
        cols: [
          {
            textAlign: 'center',
            values: ['是', '否'],
            displayValues: ['是', '否']
          },
        ],
        formatValue: function (picker, value, displayValue){
          $(this.input).parent().find('input[type="hidden"]').val(value)
          return displayValue
        },
      });
      
      $('[data-name="arthrosis"]').picker({
        toolbarTemplate: '<header class="bar bar-nav">\
          <button class="button button-link pull-left">取消</button>\
          <button class="button button-link pull-right close-picker">确定</button>\
          </header>',
        cols: [
          {
            textAlign: 'center',
            values: ['良好', '不足', '严重受限'],
            displayValues: ['良好', '不足', '严重受限']
          },
        ],
        formatValue: function (picker, value, displayValue){
          $(this.input).parent().find('input[type="hidden"]').val(value)
          return displayValue
        },
      });
      
      $('[data-class="time"]').picker({
        toolbarTemplate: '<header class="bar bar-nav">\
          <button class="button button-link pull-left">取消</button>\
          <button class="button button-link pull-right close-picker">确定</button>\
          </header>',
        cols: [
          {
            textAlign: 'center',
            values: ['1', '2', '3','4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24'],
          },
          {
            textAlign: 'center',
            values: ['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', 
                     '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30',
                     '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45',
                     '46', '47', '48', '49', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59'],
          },
        ],
        formatValue: function (picker, value, displayValue){
          $(this.input).parent().find('input[type="hidden"]').val(value);
          $(this.input).parent().find(".time").text(value[0] + ":" + value[1])
          return displayValue
        },
      })
      
      $('[data-name="standingPosture1"]').picker({
        toolbarTemplate: '<header class="bar bar-nav">\
          <button class="button button-link pull-left">取消</button>\
          <button class="button button-link pull-right close-picker">确定</button>\
          </header>',
        cols: [
          {
            textAlign: 'center',
            values: ['中立位', '前引', '后仰'],
            displayValues: ['中立位', '前引', '后仰']
          }
        ],
        formatValue: function (picker, value, displayValue){
          $(this.input).parent().find('input[type="hidden"]').val(value);
          return displayValue
        }
      })
      
      $('[data-name="standingPosture1"]').picker({
        toolbarTemplate: '<header class="bar bar-nav">\
          <button class="button button-link pull-left">取消</button>\
          <button class="button button-link pull-right close-picker">确定</button>\
          </header>',
        cols: [
          {
            textAlign: 'center',
            values: ['中立位', '前引', '后仰'],
            displayValues: ['中立位', '前引', '后仰']
          }
        ],
        formatValue: function (picker, value, displayValue){
          $(this.input).parent().find('input[type="hidden"]').val(value);
          return displayValue
        }
      })
      
      $('[data-name="standingPosture2"]').picker({
        toolbarTemplate: '<header class="bar bar-nav">\
          <button class="button button-link pull-left">取消</button>\
          <button class="button button-link pull-right close-picker">确定</button>\
          </header>',
        cols: [
          {
            textAlign: 'center',
            values: ['中立位', '圆肩'],
            displayValues: ['中立位', '圆肩'],
          }
        ],
        formatValue: function (picker, value, displayValue){
          $(this.input).parent().find('input[type="hidden"]').val(value);
          return displayValue
        }
      })
      
      $('[data-name="standingPosture3"]').picker({
        toolbarTemplate: '<header class="bar bar-nav">\
          <button class="button button-link pull-left">取消</button>\
          <button class="button button-link pull-right close-picker">确定</button>\
          </header>',
        cols: [
          {
            textAlign: 'center',
            values: ['中立位', '前倾', '后倾'],
            displayValues: ['中立位', '前倾', '后倾'],
          }
        ],
        formatValue: function (picker, value, displayValue){
          $(this.input).parent().find('input[type="hidden"]').val(value);
          return displayValue
        }
      })
      
      $('[data-name="standingPosture4"]').picker({
        toolbarTemplate: '<header class="bar bar-nav">\
          <button class="button button-link pull-left">取消</button>\
          <button class="button button-link pull-right close-picker">确定</button>\
          </header>',
        cols: [
          {
            textAlign: 'center',
            values: ['中立位', '膝外翻', '膝内翻'],
            displayValues: ['中立位', '膝外翻', '膝内翻'],
          }
        ],
        formatValue: function (picker, value, displayValue){
          $(this.input).parent().find('input[type="hidden"]').val(value);
          return displayValue
        }
      })
      
      $('[data-name="backPosture1"]').picker({
        toolbarTemplate: '<header class="bar bar-nav">\
          <button class="button button-link pull-left">取消</button>\
          <button class="button button-link pull-right close-picker">确定</button>\
          </header>',
          cols: [
            {
              textAlign: 'center',
              values: ['中立位', '侧倾'],
              displayValues: ['中立位', '侧倾']
            }
          ],
          formatValue: function (picker, value, displayValue){
            $(this.input).parent().find('input[type="hidden"]').val(value);
            return displayValue
          },
      })
      
      $('[data-name="backPosture2"]').picker({
        toolbarTemplate: '<header class="bar bar-nav">\
          <button class="button button-link pull-left">取消</button>\
          <button class="button button-link pull-right close-picker">确定</button>\
          </header>',
          cols: [
            {
              textAlign: 'center',
              values: ['中立位', '扭转'],
              displayValues: ['中立位', '扭转']
            }
          ],
          formatValue: function (picker, value, displayValue){
            $(this.input).parent().find('input[type="hidden"]').val(value);
            return displayValue
          },
      })
      
      $('[data-name="backPosture3"]').picker({
        toolbarTemplate: '<header class="bar bar-nav">\
          <button class="button button-link pull-left">取消</button>\
          <button class="button button-link pull-right close-picker">确定</button>\
          </header>',
        cols: [
          {
            textAlign: 'center',
            values: ['中立位', '左高', '右高'],
            displayValues: ['中立位', '左高', '右高']
          }
        ],
        formatValue: function (picker, value, displayValue){
          $(this.input).parent().find('input[type="hidden"]').val(value);
          return displayValue
        }
      })
      
      $('[data-name="backPosture4"]').picker({
        toolbarTemplate: '<header class="bar bar-nav">\
          <button class="button button-link pull-left">取消</button>\
          <button class="button button-link pull-right close-picker">确定</button>\
          </header>',
        cols: [
          {
            textAlign: 'center',
            values: ['中立位', '超伸'],
            displayValues: ['中立位', '超伸']
          }
        ],
        formatValue: function (picker, value, displayValue){
          $(this.input).parent().find('input[type="hidden"]').val(value);
          return displayValue
        }
      })
      
      $('[data-name="backPosture5"]').picker({
        toolbarTemplate: '<header class="bar bar-nav">\
          <button class="button button-link pull-left">取消</button>\
          <button class="button button-link pull-right close-picker">确定</button>\
          </header>',
          cols: [
            {
              textAlign: 'center',
              values: ['正常', '扁平足'],
              displayValues: ['正常', '扁平足']
            }
          ],
          formatValue: function (picker, value, displayValue){
            $(this.input).parent().find('input[type="hidden"]').val(value);
            return displayValue
          },
      })
      
      
      
      $('[data-class="frequency"]').picker({
        toolbarTemplate: '<header class="bar bar-nav">\
          <button class="button button-link pull-left">取消</button>\
          <button class="button button-link pull-right close-picker">确定</button>\
          </header>',
          cols: [
            {
              textAlign: 'center',
              values: ['1', '2', '3','4', '5', '6', '7']
            }
          ],
          formatValue: function (picker, value, displayValue){
            $(this.input).parent().find('input[type="hidden"]').val(value);
            $(this.input).parent().find(".number").text(value)
            return displayValue
          },
      })
      
    },
    
    initSubmit: function() {
      var that = this;
      
      $("form").submit({
        
      })
    }
  }
})